<?php
namespace Library;

class Response
{
	//	返回文档类型
	protected $ContentType="application/json";
	
	//	返回状态码
	protected $code;
	
	//	原始数据
	protected $data;
	
	//	字符集
	protected $charset;
	
	//	自定义返回头
	protected $header;
	
	//	构造函数
	public function __construct($data,$header="",$code=200,$charset="utf-8"){
		$this->header = $header;
		$this->code = $code;
		$this->data = $data;
		$this->charset = $charset;
		
		$this->send($this->data);
	}
	
	//	发送信息
	public function send($data){
		if ( empty($data) ){
			echo json_encode(["code"=>200,"msg"=>"数据为空"],JSON_UNESCAPED_UNICODE);
			exit;
		}
		
		//	发送请求头
		http_response_code($this->code);
		header('Access-Control-Allow-Origin: *');
		header("Cache-Control","no-cache");
		
		//	返回类型已经字符编码
		header('Content-Type:'.$this->ContentType.";charset:".$this->charset);
		
		//	发送数据
		echo $data;
		exit;
	}
	
	
	public function create(){
		$this->send($this->data);
	}
	
}
?>